package educrypt.gui.components;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import educrypt.gui.util.GuiUtil;
import educrypt.gui.view.CiphersOptionsView;
import educrypt.gui.view.TextFilterView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

import java.util.ArrayList;
import java.util.List;

class CiphersOptionsPanel extends EducryptPanel implements CiphersOptionsView {

	private static final long serialVersionUID = 1L;

	private JLabel lblCipherOptions, lblCipherType, lblAlphabet, lblKeyword;
	private JComboBox cbxCipherType, cbxAlphabet, cbxKeyword;
	private JButton btnAddAlphabet, btnAddKeyword, btnEncrypt, btnDecrypt,
			btnFilter;

	private TextFilterDialog textFilterDialog;

	public CiphersOptionsPanel() {
		this.setSize(750, 130);
		this.setPreferredSize(this.getSize());
		setLayout(null);

		lblCipherOptions = new JLabel(Messages.getString("guiCipherOptions"));
		lblCipherOptions.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblCipherOptions.setBounds(31, 11, 173, 19);
		add(lblCipherOptions);

		cbxCipherType = new JComboBox();
		cbxCipherType.setBounds(31, 57, 160, 20);
		add(cbxCipherType);

		lblCipherType = new JLabel(Messages.getString("guiCipherType"));
		lblCipherType.setBounds(31, 39, 132, 14);
		add(lblCipherType);

		cbxAlphabet = new JComboBox();
		cbxAlphabet.setBounds(268, 57, 160, 20);
		add(cbxAlphabet);

		lblAlphabet = new JLabel(Messages.getString("guiAlphabet"));
		lblAlphabet.setBounds(268, 37, 67, 14);
		add(lblAlphabet);

		btnAddAlphabet = new JButton();
		btnAddAlphabet.setBounds(430, 55, 24, 24);
		add(btnAddAlphabet);

		cbxKeyword = new JComboBox();
		cbxKeyword.setBounds(519, 57, 160, 20);
		add(cbxKeyword);

		lblKeyword = new JLabel(Messages.getString("guiKeyword"));
		lblKeyword.setBounds(519, 39, 231, 14);
		add(lblKeyword);

		btnAddKeyword = new JButton();
		btnAddKeyword.setBounds(681, 55, 24, 24);
		add(btnAddKeyword);

		ImageIcon iconAdd = new ImageIcon(
				EduCryptResources.getImageURL("plus24.png"));
		btnAddAlphabet.setIcon(iconAdd);
		btnAddKeyword.setIcon(iconAdd);

		JSeparator separator = new JSeparator();
		separator.setBounds(195, 129, 359, 1);
		add(separator);

		btnFilter = new JButton(Messages.getString("guiFilter"));
		btnFilter.setBounds(195, 88, 100, 30);
		add(btnFilter);

		btnEncrypt = new JButton(Messages.getString("guiEncrypt"));
		btnEncrypt.setBounds(325, 88, 100, 30);
		add(btnEncrypt);

		btnDecrypt = new JButton(Messages.getString("guiDecrypt"));
		btnDecrypt.setBounds(455, 88, 100, 30);
		add(btnDecrypt);

		this.setPanelEnabled(false);

		textFilterDialog = new TextFilterDialog(GuiUtil.findParentFrame(this));
	}

	public void addListener(ActionListener ctr) {
		btnAddKeyword.setActionCommand(ADD_KEYWORD);
		btnAddKeyword.addActionListener(ctr);

		btnAddAlphabet.setActionCommand(ADD_ALPHABET);
		btnAddAlphabet.addActionListener(ctr);

		cbxCipherType.setActionCommand(CIPHER_SELECTED);
		cbxCipherType.addActionListener(ctr);

		cbxAlphabet.setActionCommand(ALPHABET_SELECTED);
		cbxAlphabet.addActionListener(ctr);

		cbxKeyword.setActionCommand(KEYWORD_SELECTED);
		cbxKeyword.addActionListener(ctr);

		btnFilter.setActionCommand(FILTER_TEXT);
		btnFilter.addActionListener(ctr);

		btnEncrypt.setActionCommand(ENCRYPT);
		btnEncrypt.addActionListener(ctr);

		btnDecrypt.setActionCommand(DECRYPT);
		btnDecrypt.addActionListener(ctr);

		textFilterDialog.addListener(ctr);
	}

	@Override
	public void setPanelEnabled(boolean b) {
		super.setPanelEnabled(b);
		if (b == true) {
			cbxAlphabet.setEnabled(false);
			cbxKeyword.setEnabled(false);
			btnAddAlphabet.setEnabled(false);
			btnAddKeyword.setEnabled(false);
			btnEncrypt.setEnabled(false);
			btnDecrypt.setEnabled(false);
			btnFilter.setEnabled(false);

			cbxCipherType.setSelectedIndex(0);
		}
	}

	@Override
	public void showCiphers(List<String> ciphers) {
		GuiUtil.addItemsMaintainingSelected(cbxCipherType, ciphers);
	}

	@Override
	public void showAlphabetsIfEnabled(List<String> alphabetNames) {
		if (cbxAlphabet.isEnabled()) {
			List<String> tmp = new ArrayList<String>(alphabetNames);
			tmp.add(0, Messages.getString("guiSelectAlphabet"));
			GuiUtil.addItemsMaintainingSelected(cbxAlphabet, tmp);
		}
	}

	@Override
	public void showAlphabets(List<String> alphabetNames) {
		cbxAlphabet.setEnabled(true);
		List<String> tmp = new ArrayList<String>(alphabetNames);
		tmp.add(0, Messages.getString("guiSelectAlphabet"));
		GuiUtil.addItemsMaintainingSelected(cbxAlphabet, tmp);
		btnAddAlphabet.setEnabled(true);
	}

	@Override
	public void hideAlphabets() {
		if (cbxAlphabet.isEnabled()) {
			assert cbxAlphabet.getModel().getSize() > 0;
			cbxAlphabet.setSelectedIndex(0);
			cbxAlphabet.setEnabled(false);
		}
		btnAddAlphabet.setEnabled(false);
	}

	@Override
	public void showKeywords(List<String> keywords) {
		cbxKeyword.setEnabled(true);
		cbxKeyword.removeAllItems();
		List<String> tmp = new ArrayList<String>(keywords);
		tmp.add(0, Messages.getString("guiSelectKeyword"));
		GuiUtil.addItemsMaintainingSelected(cbxKeyword, tmp);
		btnAddKeyword.setEnabled(true);
	}

	@Override
	public void hideKeywords() {
		if (cbxKeyword.isEnabled()) {
			assert cbxAlphabet.getModel().getSize() > 0;
			cbxKeyword.setSelectedIndex(0);
			cbxKeyword.setEnabled(false);
		}
		btnAddKeyword.setEnabled(false);
	}

	@Override
	public void enableFilter(boolean show) {
		btnFilter.setEnabled(show);
	}

	@Override
	public void enableAction(boolean show) {
		btnEncrypt.setEnabled(show);
		btnDecrypt.setEnabled(show);
	}

	@Override
	public int getSelectedCipherIndex() {
		return cbxCipherType.getSelectedIndex();
	}

	@Override
	public int getSelectedAlphabetIndex() {
		return cbxAlphabet.getSelectedIndex();
	}

	@Override
	public String getSelectedAlphabetName() {
		return (String) cbxAlphabet.getSelectedItem();
	}

	@Override
	public void setSelectedAlphabetName(String alphabetName) {
		cbxAlphabet.setSelectedItem(alphabetName);
	}

	@Override
	public int getSelectedKeywordIndex() {
		return cbxKeyword.getSelectedIndex();
	}

	@Override
	public void setSelectedKeyword(String keyword) {
		cbxKeyword.setSelectedItem(keyword);
	}

	@Override
	public TextFilterView getTextFilterDialog() {
		return textFilterDialog;
	}
}
